package com.hw.shortlink.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hw.shortlink.project.common.convention.response.Result;
import com.hw.shortlink.project.common.convention.response.Results;
import com.hw.shortlink.project.dto.req.ShortLinkCreateBatchReqDto;
import com.hw.shortlink.project.dto.req.ShortLinkCreateReqDto;
import com.hw.shortlink.project.dto.req.ShortLinkPageReqDto;
import com.hw.shortlink.project.dto.req.ShortLinkUpdateReqDto;
import com.hw.shortlink.project.dto.resp.ShortLinkCreateBatchRespDto;
import com.hw.shortlink.project.dto.resp.ShortLinkCreateRespDto;
import com.hw.shortlink.project.dto.resp.ShortLinkGroupCountQueryRespDto;
import com.hw.shortlink.project.dto.resp.ShortLinkPageRespDto;
import com.hw.shortlink.project.service.ShortLinkService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkService shortLinkService;



    /**
     * 新增短锻炼
     * @param shortLinkCreateReqDTO
     * @return
     */
    @PostMapping("/api/short-link/v1/create")
    Result<ShortLinkCreateRespDto> createShortLink(@RequestBody ShortLinkCreateReqDto shortLinkCreateReqDTO){

        return Results.success(shortLinkService.createShortLink(shortLinkCreateReqDTO));
    }

    /**
     * 批量新增短锻炼
     * @param shortLinkCreateReqDTO
     * @return
     */
    @PostMapping("/api/short-link/v1/create/batch")
    Result<ShortLinkCreateBatchRespDto> createShortLinkBatch(@RequestBody ShortLinkCreateBatchReqDto shortLinkCreateBatchReqDto){
        return Results.success(shortLinkService.createShortLinkBatch(shortLinkCreateBatchReqDto));
    }

    /**
     * 分页查询
     * @param shortLinkPageReqDto
     * @return
     */
    @GetMapping("/api/short-link/v1/page")
    Result<IPage<ShortLinkPageRespDto>> pageQuery(ShortLinkPageReqDto shortLinkPageReqDto){
        return Results.success(shortLinkService.pageQuery(shortLinkPageReqDto));
    }


    /**
     * 修改短链接
     * @param shortLinkUpdateReqDTO
     * @return
     */
    @PostMapping("/api/short-link/v1/update")
    Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDto shortLinkUpdateReqDTO){

        shortLinkService.updateShortLink(shortLinkUpdateReqDTO);
        return Results.success();
    }


    /**
     * 查询短链接分组内数量
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkGroupCountQueryRespDto>> listGroupShortLinkCount(@RequestParam("requestParam") List<String> gids) {
        return Results.success(shortLinkService.listGroupShortLinkCount(gids));
    }

    /**
     * 短链接跳转到原始链接
     * @param shortUri
     * @param request
     * @param response
     */
    @GetMapping("/{shortUri}")
    public void restoreToOriginUrl(@PathVariable("shortUri") String shortUri, HttpServletRequest request, HttpServletResponse response){
        shortLinkService.redirectUrl(shortUri,request,response);
    }

}
